<?php
namespace app\houtai\controller\system\rbac;
use app\houtai\controller\BaseController;

class UserController extends BaseController {

  const STATES = [
    0 => '未禁用',
    1 => '已禁用',
  ];
  /**
   *  @brief  : 后台账号查询
   *  @return : page
   *  @detail :
   */
  public function indexAction() {
    $conds = ['IsDelete' => 0];
    $order = [];
    $ADMIN_ROLES = GetAdminRole();
    $IsDisabled = $this->request->param('IsDisabled/d', -1);
    $RoleID = $this->request->param('RoleID/d', 0);

    if ($RoleID > 0 && array_key_exists($RoleID, $ADMIN_ROLES)) {
      $conds['RoleID'] = $RoleID;
    }

    if ($IsDisabled >= 0 && array_key_exists($IsDisabled, self::STATES)) {
      $conds['IsDisabled'] = $IsDisabled;
    }
    //////////////////////////////////////
    $orderMap = [
      'A' => 'AdminId',
      'B' => 'RoleID',
      'C' => 'UserName',
      'D' => 'NickName',
      'E' => 'Email',
      'F' => 'Mobile',
      'G' => 'LoginNum',
      'H' => 'IsDisabled',
      'I' => 'LastLoginIP',
      'J' => 'LastLoginTime',
    ];
    $this->orderField = array_key_exists($this->orderField, $orderMap) ? $this->orderField : 'G';
    $order[$orderMap[$this->orderField]] = $this->orderDirection;
    //////////////////////////////////////
    $list = table('Ky_Admin', 'rydb.websms')->where($conds)->order($order)->paginate();
    ////////////////////////////////////////////////////////////////////////////
    return $this->fetch('', [
      'STATES'      => self::STATES,
      'IsDisabled'  => $IsDisabled,
      'ADMIN_ROLES' => $ADMIN_ROLES,
      'RoleID'      => $RoleID,
      'list'        => $list,
    ]);
  }
  /**
   *  @brief  : 账号详情
   *  @return : page|JSON
   *  @detail :
   */
  public function detailAction() {
    $AdminID      = $this->request->param('AdminID/d', 0);
    $ADMIN_ROLES  = GetAdminRole();
    $Admin = [];
    
    if ($AdminID > 0) {
      $Admin = table('Ky_Admin', 'rydb.websms')->where('AdminId', $AdminID)->find();
    }

    if ($this->request->isGet()) {
      return $this->fetch('', [
        'Admin'   => $Admin,
        'AdminID' => $AdminID,
        'ADMIN_ROLES' => $ADMIN_ROLES,
      ]);
    }

    if ($this->request->isPost()) {

      $param = $this->request->only('RoleID,IsDisabled,UserName,PassWord,NickName,Email,Mobile');
      //--- 验证器验证 ---
      $eax = implode(',', array_keys($ADMIN_ROLES));
      $ebx = implode(',', array_keys(self::STATES));
      $retn = $this->validate($param, [ // 按照顺序逐一判断
        'RoleID|所属角色'     => "require|in:{$eax}",
        'IsDisabled|是否禁用' => "in:{$ebx}",
        'UserName|登陆账号'   => 'require|max:20',
        'PassWord|登陆密码'   => ($AdminID == 0 ? 'require|' : '').'max:255',
        'NickName|昵称'       => 'require|max:20',
        'Email|邮箱'          => 'email',
        'Mobile|手机号码'     => 'max:11',
      ]);
      
      if (true !== $retn) {
        return result([], 100, $retn);
      }

      if (empty($param['PassWord'])) {
        unset($param['PassWord']);
      } else {
        $param['PassWord'] = password_hash($param['PassWord'], PASSWORD_BCRYPT);
      }

      if ($AdminID > 0) { // 更新
        if (empty($Admin)) {
          return result([], 102, "用户不存在!AdminID={$AdminID}");
        }
        if ($Admin['IsSys'] == 1 && $this->auid != $AdminID) {
          return result([], 103, "无法修改系统账号"); // 系统账号只能自己登进去改, 以防万一
        }
        $eax = table('Ky_Admin', 'rydb.websms')->where(['AdminId' => ['NEQ', $AdminID], 'UserName' => $param['UserName']])->count();
        if ($eax > 0) {
          return result([], 104, "已存在同登录名!UserName={$param['UserName']}");
        }
        $eax = table('Ky_Admin', 'rydb.websms')->where('AdminId', $AdminID)->update($param);
      } else { // 创建
        $eax = table('Ky_Admin', 'rydb.websms')->insert($param);
      }

      return result([], 0);
    }
  }
  public function deleteAction() {
    $AdminID = $this->request->param('AdminID/d', 0);
    $Admin = table('Ky_Admin', 'rydb.websms')->where('AdminId', $AdminID)->find();
    if (empty($Admin)) {
      return abort(1, "用户[AdminID={$AdminID}]不存在");
    }
    if ($Admin['IsSys'] > 0) {
      return abort(1, "系统账号[{$Admin['UserName']}]不能删除");
    }
    $eax = table('Ky_Admin', 'rydb.websms')->where('AdminId', $AdminID)->delete ();
    $eax = table('Ky_AdminLog', 'rydb.websms')->where('AdminId', $AdminID)->delete ();
    return result([], 0);
  }
}
